//
//  DDWelcomeJobsViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/4/10.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDWelcomeJobsViewController.h"
#import "DDJobsService.h"
#import "DDJobsBaseModel.h"
#import "DDDiscoverJobCell.h"
#import "DDWorkDetailViewController.h"

//NSString *DDDiscoverJobCellIdentifier = @"DDDiscoverJobCell";
@interface DDWelcomeJobsViewController ()

@property (strong, nonatomic) DDJobsService *jobsService;

@end



@implementation DDWelcomeJobsViewController

#pragma mark
#pragma mark - View LifeCycle

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.jobsService = [[DDJobsService alloc] init];

        self.startIndexPage = 0;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupData];
    [self setupView];
}


#pragma mark
#pragma mark - Private Methods

- (void)setupData
{
    [self requestJobsData];
}

- (void)setupView
{
    [self configureTableView];
    
}
- (void)configureTableView
{
    self.tableView.rowHeight = HEIGHT_DISCOVER_CELL;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    self.tableView.contentInset = UIEdgeInsetsMake(2, 0, 10, 0);
    
    UINib *cellNib = [UINib nibWithNibName:@"DDDiscoverJobCell" bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:DDDiscoverJobCellIdentifier];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addRefreshHeader];
    [self addRefreshFooter];
}

- (void)loadNewData
{
    self.startIndexPage = 0;
    [self requestJobsData];
}

- (void)loadMoreData
{
    self.startIndexPage ++;
    [self requestJobsData];
}


#pragma mark
#pragma mark - Data

/**
 *  未登录状态最多能展示15条， index从0开始
 */
- (void)requestJobsData
{
    if (self.startIndexPage == 0) {
        [self.dataSource removeAllObjects];
//        [self showLoading];
    }
    
    WEAKSELF
    [self.jobsService queryJobsWithPage:[NSString stringWithFormat:@"%d",(int)self.startIndexPage] successBlock:^(id responseObject) {
        [weakSelf hideLoading];
        if ([weakSelf isValidateResponseData:responseObject]) {
            // 请求成功
            DDJobsBaseModel *model = [DDJobsBaseModel yy_modelWithJSON:responseObject[@"data"]];
            
            [weakSelf.dataSource addObjectsFromArray:model.jobs];
            [weakSelf.tableView reloadData];
            
            //上拉刷新状态
            if ([weakSelf isRefreshing]) {
                [self setFooterHidden:model.jobs.count < self.pageCount ? YES : NO];
                
                [self endRefreshHeader];
                
            } else {
                //下拉刷新
                if ([model.jobs count] < self.pageCount) {
                    [self setFooterHidden:YES];
                }
                [self endRefreshFooter];
            }
            
            if (self.startIndexPage == 1) {
                // 隐藏上拉加载
                [self setFooterHidden:YES];
            }
            
        } else {
            [weakSelf responseUnValidate:responseObject];
        }
        
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDDiscoverJobCell *cell = (DDDiscoverJobCell *)[tableView dequeueReusableCellWithIdentifier:DDDiscoverJobCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if ([self.dataSource count] > indexPath.row) {
        [cell updateViewWithData:(DDJobsDetail *)self.dataSource[indexPath.row]];
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  182;
}
#pragma mark -
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DDJobsDetail *detail = (DDJobsDetail *)self.dataSource[indexPath.row];
    
    DDWorkDetailViewController *workVC = [[DDWorkDetailViewController alloc] init];
    workVC.title = detail.jobtitle;
    workVC.jobDetail = detail;
    workVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:workVC animated:YES];
}

@end
